#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: timeouts.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Icon manager for Vazaar

import os

import gobject
import gtk

from vazaarlib.constants import IMAGES_DIR
from vazaarlib.utils import get_logger

class TimeOutsManager():
    def __init__(self, gui):
        self.gui = gui
        self.log = get_logger("Timeouts")
        self.timeout = {}

        self.timeout['update-gui'] = [5000, self.gui.actions.update_gui] #gobject.timeout_add(5000, self.gui.actions.update_gui)

    def stop_all(self):
        for name in self.timeout:
            print "trying to remove timeout", name,
            try:
                gobject.source_remove(self.timeout[name][2])
                print "Ok."
            except Exception, error:
                print error

    def stop(self, widget, event, name):
        try:
            gobject.source_remove(self.timeout[name])
            print "removing timeout", name
        except Exception, error:
            self.log.error(error)

    def start(self, widget, event, name):
        try:
            timeout = self.timeout[name][0]
            function = self.timeout[name][1]
            self.timeout[name][2] = gobject.timeout_add(timeout, function)
            print "starting timeout", name, timeout, function
        except Exception, error:
            self.log.error(error)

"""
    def start(self, name, callback, miliseconds=1000):
        self.timeout[name] = gobject.timeout_add(miliseconds, callback)
"""
